/*
                        *********************
*************************** C SOURCE FILE ************************************
**                      *********************                               **
**                                                                          **
*/

#define _SERVER_C_SRC

/****************************************************************************/
/**                                                                        **/
/** MODULES USED                                                           **/
/**                                                                        **/
/****************************************************************************/

#include "server.h"

/****************************************************************************/
/**                                                                        **/
/** DEFINITIONS AND MACROS                                                 **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** TYPEDEFS AND STRUCTURES                                                **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** PROTOTYPES OF LOCAL FUNCTIONS                                          **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** EXPORTED VARIABLES                                                     **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** GLOBAL VARIABLES                                                       **/
/**                                                                        **/
/****************************************************************************/

FILE *text;
FILE *list;

/****************************************************************************/
/**                                                                        **/
/** EXPORTED FUNCTIONS                                                     **/
/**                                                                        **/
/****************************************************************************/



/****************************************************************************/
/**                                                                        **/
/** LOCAL FUNCTIONS                                                        **/
/**                                                                        **/
/****************************************************************************/
int main()
{
	int16s 	server_sockfd, client_sockfd;
	int16s 	server_len, client_len;
	struct 	sockaddr_in server_address;
	struct 	sockaddr_in client_address;
	int16s 	result;
	int16s	i,j,check;
	int16u	answer,nB,round = 0;
	int16u	state=READ,N;
	int8u	buf[200],*filename,dataname[50];

	/* Create an unnamed socket for the server. */
	server_sockfd = socket(AF_INET, SOCK_STREAM, 0);

	/* Assign a name to a socket. */
	server_address.sin_family = AF_INET;
	server_address.sin_addr.s_addr = inet_addr("192.168.1.245");
	server_address.sin_port = 22;
	server_len = sizeof(server_address);
	result = bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
	if(result == -1)
	{
		perror("OOPS!!: bind function error\n");
		exit(1);
	}
		
	/* Create a connection queue and wait for clients. */
	result = listen(server_sockfd, 5);
	if(result == -1)
	{
		perror("OOPS!!: listen function error\n");
		exit(1);
	}

	/* Accept a connection. */
	client_len = sizeof(client_address);
	client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);

	list = fopen("name","a");
	fclose(list);

	while(state != NON)
	{
		switch(state)
		{
			case READ :printf("READ DATA\r\n");
				clr(buf,200);
				result = read(client_sockfd,buf,200);printf("%s\r\n",buf);
				if(result == -1)
				{
					printf("get DATA Error\n");
					answer = 0;
					write(client_sockfd,&answer,sizeof(int16u));
					state = NON;
				}
				else
				{
					printf("read data is %s\r\n",buf);

					i=0;
					while(i<200)
					{
						if(buf[i]==0x0D)
						{
							printf("before CR\r\n");
						}
						i++;
					}
					i=0;
					while(i<200)
					{
						if(buf[i]==0x0A)
						{
							printf("before LF\r\n");
						}
						i++;
					}
			
					
		if((buf[0]=='l')&&(buf[1]=='s')&&(buf[2]=='\0'))
		{
			state = LS;

		}
		else if((buf[0]=='c')&&(buf[1]=='a')&&(buf[2]=='t')&&(buf[3]==' ')&&(buf[4]!='\0'))
		{
			state = CAT;
		}
		else if((buf[0]=='v')&&(buf[1]=='i')&&(buf[2]==' ')&&(buf[3]!='\0'))
		{
			state = VI;
			write(client_sockfd,"PLEASE WRITE DATA FOR THIS FILE\n\r",34*sizeof(int8u));
		}
		else  
		{
			write(client_sockfd,"NOT COMMAND\n\r",34*sizeof(int8u));
		}
			}
			break;
			case LS :printf("%c%c\n",buf[0],buf[1]);
				clr(buf,200);
				list = fopen("name","r");
				fread(buf,200,1,list);
				printf("List directory\r\n%s\r\n",buf);
				write(client_sockfd,buf,200);
				state = READ;
			break;
			case CAT :printf("%c%c%c\n",buf[0],buf[1],buf[2]);
				i=4;j=0;
				while(buf[i]!='\0')
				{
					i++;
					j++;
				}
				filename = (int8u*)malloc((j+1)*sizeof(int8u));
				i=4;j=0;
				while(!((buf[i]=='\0')||(buf[i]==0x0D)||(buf[i]==0x0A)))
				{
					filename[j] = buf[i];
					i++;
					j++;
				}
				printf("%s\n",filename);
				clr(buf,200);
				text = fopen(filename,"r");
				fread(buf,200,1,text);
				printf("%s\r\n",buf);
				write(client_sockfd,buf,200);
				free(filename);
				state = READ;	
			break;
			case VI :printf("%c%c\n",buf[0],buf[1]);
				i=3;j=0;
				while(buf[i]!='\0')
				{
					i++;
					j++;
				}
				filename = (int8u*)malloc((j+1)*sizeof(int8u));
				i=3;j=0;
				while(!((buf[i]=='\0')||(buf[i]==0x0D)||(buf[i]==0x0A)))
				{
					filename[j] = buf[i];
					i++;
					j++;
				}
				printf("%s\n",filename);
				list = fopen("name","r");
				check = fscanf(list,"%s",dataname);
				if(check == EOF)
				{
					N = 0;printf("filename empty\n");
				}
				else
				{	
					rewind(list);
					while((fscanf(list,"%s",dataname))!=EOF)
					{	printf("check filename in list\n");
						printf("%s\n",dataname);
						i=0;j=0;
						while(dataname[i]!='\0')
						{
							if(filename[i]==dataname[i])
							{
								j++;
								printf("%d",j);
							}
							i++;
						}
						if(j==i)
						{
							N = 1;printf("\nOld file\n");
						}
						else
						{
							N = 0;printf("\nNew file\n");
						}
					}
				}
				fclose(list);

				if(N==0)
				{
					list = fopen("name","a");
					fprintf(list,"%s\r\n",filename);printf("Save filename\n");
					fclose(list);
				}

				text = fopen(filename,"a");
				clr(buf,200);printf("\nPLEASE WRITE DATA TO \"%s\" FILE\n",filename);
				read(client_sockfd,buf,200);printf("%s\r\n",buf);
				if(result == -1)
				{
					printf("get DATA Error\r\n");
					fclose(text);
				}
				else
				{
					fprintf(text,"%s\r\n",buf);
					i=0;
					while(i<200)
					{
						if(buf[i]==0x0D)
						{
							printf("before CR\r\n");
						}
						i++;
					}
					i=0;
					while(i<200)
					{
						if(buf[i]==0x0A)
						{
							printf("before LF\r\n");
						}
						i++;
					}
					fclose(text);
					clr(buf,200);
					text = fopen(filename,"r");
					fread(buf,200,1,text);
					printf("%s\r\n",buf);
					write(client_sockfd,buf,200);
				}
				state = READ;
				free(filename);
			break;
//			default :
		}
	}
	/* Close socket. */
	close(client_sockfd);
	printf("close socket\n");

	return 0;
}



/****************************************************************************/
/**                                                                        **/
/** EOF                                                                    **/
/**                                                                        **/
/****************************************************************************/
 
